package lk.cms.all.service.impl;

/**
 * @author LK
 * @date 2020/9/5 - 22:10
 */
import java.util.List;
import java.util.Optional;

import lk.cms.all.dao.AllDao;
import lk.cms.all.domain.All;
import lk.cms.all.service.AllManager;
import lk.cms.base.service.impl.GenericManagerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



/**
 * ManagerImpl for All
 *
 * @author 42276
 *
 */

//@Transactional
@Service // (value = "allManager")
public class AllManagerImpl extends GenericManagerImpl<All, Long> implements AllManager {

    AllDao allDao;

    @Autowired
    public void setAllDao(AllDao allDao) {
        this.allDao = allDao;
        this.dao = this.allDao;
    }
    //	public static SqlSession getSqlSession() throws IOException {
//		InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
//		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//		return sqlSessionFactory.openSession();
//	}
//	@Override
//	public List<All> getList() {
//		return allDao.selectAlls();
//	}
    @Override
    public All getAllById(Long id) {
        return allDao.selectById(id);
    }
    @Override
    public boolean IfExist(String username) {
        //数据存在返回false
        //数据不存在返回true
        if((allDao.IfOneExist(username))>0)
            return false;
        else
            return true;
    }
    @Override
    public void CreateOne(String username, String password, String identity) {
        All all=new All();
        all.setUsername(username);
        all.setPassword(password);
        all.setIdentity(identity);
        allDao.save(all);
    }
    @Override
    public List<All> getList() {
        return (List<All>)this.dao.findAll();
    }
    @Override
    public Optional<All> findByAllId(Long allId) {
        Optional<All> result=this.allDao.findById(allId);
        return result;
    }
    @Override
    public Boolean deleteById(Long allId) {
        Optional<All> all=this.dao.findById(allId);
        if(all.equals(Optional.empty())) {
            return false;
        }else {
            this.dao.delete(all.get());
            return true;
        }

    }


}
